//
//  TestView.m
//  RoolBK
//
//  Created by paykee on 16/6/30.
//  Copyright © 2016年 jpy. All rights reserved.
//

#define pieXChartView  self.frame.size.width / 3.2 - sqrt((self.backgroundLayer.cornerRadius * 1.1 *  self.backgroundLayer.cornerRadius * 1.1) / 2)
#define pieYChartView  self.frame.size.width / 3.2 - sqrt((self.backgroundLayer.cornerRadius * 1.1 * self.backgroundLayer.cornerRadius * 1.1) / 2)
#import "WHBKPieView.h"


@interface WHBKPieView()<CAAnimationDelegate>

@property(nonatomic,strong) CALayer *backgroundLayer;

@property (nonatomic,copy) NSString * titleStr;//标题
@property (nonatomic,copy) NSString * pingjieStr;//拼接标题
@property (nonatomic,strong) NSMutableAttributedString * stra;


@end

@implementation WHBKPieView

-(void)setDataResource:(NSArray *)dataResource{
    _dataResource = dataResource;
    
    self.pieChartView.dataResource = _dataResource;
    DLog(@"%@",self.dataResource);
    
      self.pieChartView.dataResource = self.dataResource;
    
    for (int i = 0 ; i < 3; i++) {
     UILabel * valueLabel = (UILabel *)[self viewWithTag:i+100];
                if (i == 0) {
                   
                    float num = [[NSString stringWithFormat:@"%@",_dataResource[0]]floatValue];
                    DLog(@"%@",_dataResource[0]);
                    if (num > 1000) {
                          valueLabel.text = [NSString stringWithFormat:@"投诉单:%.1f千单",num/1000.0];
        
                    }else{
                     valueLabel.text = [NSString stringWithFormat:@"投诉单:%@单",_dataResource[0]];
                    }
        
                }else if (i == 1){
        
                    float num = [[NSString stringWithFormat:@"%@",dataResource[1]]floatValue];
                    if (num > 1000) {
                        valueLabel.text = [NSString stringWithFormat:@"公共报修:%.1f千单",num/1000.0];
                    }else{
                        valueLabel.text = [NSString stringWithFormat:@"公共报修:%@单",dataResource[1]];
                    }
        
                }else if (i == 2){
                    float num = [[NSString stringWithFormat:@"%@",dataResource[2]]floatValue];
                    if (num > 1000) {
                        valueLabel.text = [NSString stringWithFormat:@"水电报修:%.1f千单",num/1000.0];
                    }else{
                        valueLabel.text = [NSString stringWithFormat:@"水电报修:%@单",dataResource[2]];
                    }
        
                }
        
                NSRange range = [valueLabel.text rangeOfString: dataResource[i]];
                NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: valueLabel.text];
                [attribute addAttributes: @{NSForegroundColorAttributeName: Theme_Color}range: range];
        
                [valueLabel setAttributedText: attribute];
    
    }
       //饼图表格提示
   // [self addPieValuePView];
    
    
    [self.pieChartView removeFromSuperview];
    //饼图
    [self addPieChartView];
    
}

-(CALayer *)backgroundLayer{
    if(!_backgroundLayer){
        _backgroundLayer = [[CALayer alloc] init];
        _backgroundLayer.cornerRadius = self.frame.size.width / 4;
        _backgroundLayer.frame = CGRectMake(0, 0, _backgroundLayer.cornerRadius * 2 , _backgroundLayer.cornerRadius * 2);
       // _backgroundLayer.borderWidth = 1;
        _backgroundLayer.position = CGPointMake(self.frame.size.width / 3.2, self.frame.size.width / 3.2 - 15);
        _backgroundLayer.borderColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:0.8].CGColor;
        _backgroundLayer.backgroundColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0].CGColor;
        _backgroundLayer.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.1].CGColor;

    }
    return _backgroundLayer;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        //饼图表格提示
        // [self addPieValuePView];
        
//        //饼图
//        [self addPieChartView];
        //饼图值
        [self addSubViews];

    }
    return self;
}

-(void)addPieChartView{
    //添加背景图层
    [self.layer addSublayer:self.backgroundLayer];
    //开始图层动画
    [self startAnimationWithStaticLayer:self.backgroundLayer];
    
    self.pieChartView = [[WHBKPieChartView alloc]initWithFrame:CGRectMake(pieXChartView - 2, pieYChartView - 17, self.backgroundLayer.cornerRadius * 1.6 , self.backgroundLayer.cornerRadius * 1.6)];
    self.titleLabel.text = self.pieChartView.titleLabel.text;
    
    self.pieChartView.backgroundColor = [BHColor(102, 102, 102)colorWithAlphaComponent:0.05];
   
    [self addSubview:self.pieChartView];
    
    self.pieChartView.dataResource = self.dataResource;
   
}

-(void)startAnimationWithStaticLayer:(CALayer *)staticLayer{
    CAAnimationGroup *animaTionGroup = [CAAnimationGroup animation];
    animaTionGroup.delegate = self;
    animaTionGroup.duration = 1;
    animaTionGroup.removedOnCompletion = YES;
    animaTionGroup.autoreverses = YES;
    animaTionGroup.repeatCount = MAXFLOAT;
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
    scaleAnimation.fromValue = @0.90;
    scaleAnimation.toValue = @0.96;
    scaleAnimation.duration = 1;
    scaleAnimation.repeatCount = MAXFLOAT;
    scaleAnimation.autoreverses = YES;
    
    CAKeyframeAnimation *opencityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    opencityAnimation.duration = 1;
    opencityAnimation.values = @[@0.4,@0.8,@0.4];
    opencityAnimation.keyTimes = @[@0,@0.5,@1];
    opencityAnimation.removedOnCompletion = YES;
    opencityAnimation.autoreverses = YES;
    opencityAnimation.repeatCount = MAXFLOAT;
    
    NSArray *animations = @[scaleAnimation];
    animaTionGroup.animations = animations;
    [staticLayer addAnimation:animaTionGroup forKey:@"groupAnnimation"];
}


-(void)addSubViews{
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(pieXChartView + self.backgroundLayer.cornerRadius * 2.1 - 10, pieXChartView - 15 + 10 , self.frame.size.width - pieXChartView + self.backgroundLayer.cornerRadius * 1.8 , 22)];
    titleLabel.text = @"";
    titleLabel.textColor = [UIColor lightGrayColor];
    titleLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:titleLabel];
    
    CGFloat blockY = 0;
    for (int i = 0 ; i < 3; i++) {
        blockY = CGRectGetMaxY(titleLabel.frame) + i * 25 + 10;
        UIView *blockView = [[UIView alloc]initWithFrame:CGRectMake(titleLabel.frame.origin.x- 10, blockY, 10, 10)];
        blockView.backgroundColor = [self colorWithIndex:i];
        [self addSubview:blockView];
        
        UILabel * valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(blockView.frame) + 10, blockY - 5, 150, 20)];
        valueLabel.tag = i+100;

        valueLabel.font = [UIFont systemFontOfSize:15];
        valueLabel.textColor = BHColor(102, 102, 102);
        [valueLabel setText: valueLabel.text];
        [self addSubview:valueLabel];
       
    }
}

-(void)addPieValuePView{
    
    UIView *listView = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.pieChartView.frame) + 20, [UIScreen mainScreen].bounds.size.width - 35 , 68)];
    listView.layer.cornerRadius = 3;
    listView.layer.masksToBounds = YES;
    listView.layer.borderColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0].CGColor;
    listView.layer.borderWidth = 0.5;
    [self addSubview:listView];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat promptBlockViewH = listView.frame.size.height / 2;
    CGFloat promptBlockViewW = listView.frame.size.width / 2;
    for (int i = 0 ; i < 4 ; i++) {
        if(i / 2 == 1){
            y = promptBlockViewH;
            x = 0;
        }
        if(i % 2 == 1){
            x = promptBlockViewW;
        }
        UIView *promptBlockView = [[UIView alloc]initWithFrame:CGRectMake(x, y, promptBlockViewW, promptBlockViewH)];

        promptBlockView.layer.borderWidth = 0.3;
        promptBlockView.layer.borderColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0].CGColor;
        [listView addSubview:promptBlockView];
        
        NSMutableDictionary *dic = [self getPromptDicValueWithIndex:i];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, promptBlockViewH * 0.15, 50, promptBlockViewH * 0.7)];
        titleLabel.text = dic[@"title"];
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.textColor = [self colorWithIndex:i];
        [promptBlockView addSubview:titleLabel];
        
        UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), promptBlockViewH * 0.15, promptBlockView.frame.size.width - 70 , promptBlockViewH * 0.7)];
        valueLabel.text = dic[@"value"];
        valueLabel.font = [UIFont systemFontOfSize:13];
        valueLabel.textColor = [UIColor lightGrayColor];
        valueLabel.textAlignment = NSTextAlignmentRight;
        [promptBlockView addSubview:valueLabel];
    }
    
}

-(UIColor *)colorWithIndex:(int)num{
    UIColor *color;
    switch (num) {
        case 0:
            color = BHColor(198, 198, 198);
            break;
        case 1:
            color = BHColor(215, 152, 3);
            break;
        case 2:
            color = Theme_Color;
            break;
        case 3:
            color = [UIColor colorWithRed:64.0/255.0 green:181.0/255.0 blue:211.0/255.0 alpha:1.0];
            break;
        default:
            break;
    }
    return color;
}

-(NSMutableDictionary *)getPromptDicValueWithIndex:(int)index{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    switch (index) {
        case 0:
            [dic setObject:@"鲜肉范" forKey:@"title"];
            [dic setObject:@"25岁及以下" forKey:@"value"];
            break;
        case 1:
            [dic setObject:@"文艺范" forKey:@"title"];
            [dic setObject:@"25岁-35岁" forKey:@"value"];
            break;
        case 2:
            [dic setObject:@"小资范" forKey:@"title"];
            [dic setObject:@"35岁-45岁" forKey:@"value"];
            break;
        case 3:
            [dic setObject:@"范儿" forKey:@"title"];
            [dic setObject:@"46岁及以上" forKey:@"value"];
            break;
        default:
            break;
    }
    return dic;
}

@end
